using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.App.Shop.Model;

namespace PlugNT.App.Shop.DAL
{
    public class Delivery
    {
        internal static string currTableName = WebConfig.TablePrefix + "s_delivery";



        public bool Add(DeliveryInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + " (");
            strSql.Append("[id],sys_model_id,[key],[name],[fare],[enable],[description],arr_str)");
            strSql.Append(" values (");
            strSql.Append(" " + model.id.ToString() + ",");
            strSql.Append(" " + model.sys_model_id.ToString() + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.key) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.name) + "',");
            strSql.Append(" " + model.fare.ToString() + ",");
            strSql.Append(" " + (model.enable ? "1" : "0") + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.description) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.arr_str) + "'");
            
            strSql.Append(")");

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }


        public bool Update(DeliveryInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[key]='" + PlugNT.Safe.StringHelper.SqlFilter(model.key) + "',");
            strSql.Append("[name]='" + PlugNT.Safe.StringHelper.SqlFilter(model.name) + "',");
            strSql.Append("[fare]=" + model.fare.ToString() + ",");
            strSql.Append("[enable]=" + (model.enable ? "1" : "0") + ",");
            strSql.Append("[description]='" + PlugNT.Safe.StringHelper.SqlFilter(model.description) + "'");
            strSql.Append("arr_str='" + PlugNT.Safe.StringHelper.SqlFilter(model.arr_str) + "'");
            strSql.Append(" where id=" + model.id.ToString());

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }

        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [" + currTableName + "] ");
            strSql.Append(" where id=" + id.ToString());

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }




        private static IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }



        public  DataTable GetList(int sysModelId, string andWhere)
        {
            return DbHelper.GetDataTable(string.Format("select * from [" + currTableName + "] where sys_model_id={0} {1}", sysModelId, andWhere));
        }



        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id=" + sysModelId + andWhere;
            object obj = DbHelper.GetSingle(sql);
            return obj == null ? 0 : int.Parse(obj.ToString());
        }

        public int GetMaxId()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 max(id) from " + currTableName);
            object obj = DbHelper.GetSingle(strSql.ToString());
            return obj == null ? 0 : int.Parse(obj.ToString());
        }

        public DeliveryInfo GetDeliveryInfo(int id)
        {
            DeliveryInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id.ToString());
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new DeliveryInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.key = dr["key"].ToString();
                    model.name = dr["name"].ToString();
                    model.fare = double.Parse(dr["fare"].ToString());
                    model.enable = dr["enable"].ToString() == "1";
                    model.description = dr["description"].ToString();
                    model.arr_str = dr["arr_str"].ToString();
                }
                dr.Close();
            }
            return model;

        }



    }
}

